package cn.gongxi.weibo.controller;

import cn.gongxi.weibo.common.response.JsonResult;
import cn.gongxi.weibo.common.response.StatusCode;
import cn.gongxi.weibo.pojo.dto.CommentDTO;
import cn.gongxi.weibo.pojo.entity.Comment;
import cn.gongxi.weibo.pojo.mapper.CommentMapper;
import cn.gongxi.weibo.pojo.vo.CommentVO;
import cn.gongxi.weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Api(tags = "03.评论模块")
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {

    private static final Logger log = LoggerFactory.getLogger(CommentController.class);
    // 自动装配
    @Autowired
    private CommentMapper commentMapper;
    // 发布评论
    @ApiOperation(value = "发布评论")
    @PostMapping("insert")
    public JsonResult insertComment(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {
        // 发布评论接口
        // 发布评论需要验证客户登录状态,获取登录用户id
        // 与登录接口名称一致
        UserVO userVO = (UserVO) session.getAttribute("user");

//        if (userVO == null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }

        long userId = userVO.getId();

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(userId);
        comment.setCreated(new Date());

        commentMapper.insertComment(comment);
        //
        return JsonResult.success();
    }
    // 获取微博列表 id:weiboId
    @ApiOperation(value = "通过id获取微博列表")
    @GetMapping ("selectByWeiboId")
    @ApiImplicitParam(name = "id",value = "微博id",required = true,dataType = "long")
    // id与传递的id一致 ，当有多个参数需要描述的的时候
    /* 使用@ApiImplicitParams(value = {
        @ApiImplicitParam(name = "id",value = "微博id",required = true,dataType = "long")
        @ApiImplicitParam(name = "username",value = "用户名",required = true)  // 当参数本身就为String类型时就不需要添加dataType属性
        }
     )*/
    public JsonResult selectByWeiboId(long id) {
        List<CommentVO> commentVOS = commentMapper.selectByWeiboId(id);
        log.debug("commentVOS:{}",commentVOS);
        return JsonResult.success(commentVOS);
    }
}
